<template>
	<view class="loginBox">
		<view class="back"></view>
		<view style="height: 250rpx;"></view>
		<view class="name">欢迎登录禹冰之家</view>
		<view class="buttonBox">
			<image class="logo" mode="widthFix" src="../../static/yblogo.png"></image>
			
			<view class="oneOver" @click="oneLogin">一键登录</view>
			<view class="agreement">
				<image v-if="isagree==0" @click="choose(1)" style="width:36rpx;height: 36rpx;margin-right: 14rpx;margin-top: 2rpx;" src="../../static/agreed.png" mode="aspectFill"></image>
				<image v-if="isagree==1" @click="choose(0)" style="width:36rpx;height: 36rpx;margin-right: 14rpx;margin-top: 2rpx;" src="../../static/isagreed.png" mode="aspectFill"></image>
				<view class="text">同意并登录<text style="color: #3381FF;" @click="goAgree(1)" >【服务协议】</text>和<text style="color: #3381FF;" @click="goAgree(2)">【隐私保护指引】</text></view>
			</view>
			<view class="tips">已有账号？<span class="register" @click="toPass">点击账号登录</span></view>
			<view class="tips" style="margin-top:20rpx"><span class="register" @click="goshifu">登录师傅端</span></view>
		</view>
	</view>
</template>

<script setup>
	import {reactive,ref,getCurrentInstance} from 'vue'
	import {onLoad} from "@dcloudio/uni-app"
	import {userStore} from '@/store/user.js'//引入空间
	const userStores = userStore()//给空间命名
	const { access_tokenFun } = userStores;//解构函数
	const {proxy} = getCurrentInstance()

  const goshifu=()=>{
		uni.navigateTo({
			url:'/pages-shifu/login/passwordlogin'
		})
	}
  const isagree =ref(0)
	//同意隐私条款
  const choose=(e)=>{
		isagree.value = e
	}
	//查看协议
	const goAgree=(id)=>{
		uni.navigateTo({
			url:'/pages-login/agreement/agreement?id='+id
		})
	}
	const oneLogin=()=>{
		if(isagree.value==0){
			proxy.$showToast('请先查看并同意隐私服务条款')
			return
		}
		uni.showLoading({
			title: '加载中'
		})
		
		uni.login({
			provider: 'weixin',
			success:(loginRes)=> {
				console.log(loginRes);
				var data = {
					code:loginRes.code
				}
				wxLogin(data)
		  }
		})
	}
	const wxLogin= async(data)=>{
		const res= await proxy.$api.login(data)
		console.log(res);
		if(res.code==200){
			access_tokenFun(res.data.access_token)
			uni.setStorageSync('access_token',res.data.access_token)
			uni.setStorageSync('istype','客户端')
			if( uni.getStorageSync('access_token')){
				getUser()
			}
			
			uni.hideLoading()
			proxy.$showToast('登陆成功')
		}else{
			uni.hideLoading()
			proxy.$showToast(err.msg)
		}
		
	}
	const getUser=async()=>{
		const res =await proxy.$api.getUser()
		if(res.code==200){
			if(res.user.phonenumber==''){
				proxy.$showToast('请先绑定手机号')
				setTimeout(()=>{
					uni.reLaunch({
						url:'/pages-user/setphone/setphone?id='+'绑定你的手机号'
					})
				},1000)
				return
			}else{
				setTimeout(()=>{
					uni.reLaunch({
					  url:'/pages/home/home?istype='+'客户端'
					})
				},1000)
			}
		}
	}
	const toPass=()=> {
		uni.navigateTo({
			url: '/pages-login/password/password'
		})
	}
	const toRegister=()=> {
		uni.navigateTo({
			url: '/pages-login/register/register'
		})
	}

</script>

<style lang="scss">
	@font-face {
		font-family: "阿里妈妈数黑体";
		font-weight: 700;
		src: url("//at.alicdn.com/wf/webfont/Z6NGK12PNmYL/PQCnFJB5enWt.woff2") format("woff2"),
			url("//at.alicdn.com/wf/webfont/Z6NGK12PNmYL/CaLgXE8NhYHt.woff") format("woff");
		font-display: swap;
	}



	.loginBox {
		height: 100vh;
		background-color: white;
		position: relative;
		z-index: +1;
		background-color: $MainBackColor;

		.agreement {
			display: flex;
			margin-top: 10rpx;
			align-items: center;
			justify-content: center;
			margin-bottom: 20px;
			width: 600rpx;


			.checkBox {
				border-radius: 50%;
				border: 2px solid $blue;
				width: 8px;
				height: 8px;
				margin-right: 5px;
			}

			.text {
				font-size: 14px;

			}
		}

		.name {
			display: flex;
			align-items: center;
			justify-content: center;
			font-family: '阿里妈妈数黑体';
			color: white;
			font-size: 60rpx;
			
		}

		.buttonBox {
			width: 690rpx;
			background-color: white;
			margin: 0 auto;
			margin-top: 80rpx;
			border-radius: 10px;
			padding-bottom: 60rpx;box-sizing: border-box;
			display: flex;
			flex-direction: column;
			align-items: center;
			

			.logo {
				width: 200rpx;
				margin: 20px 0px;
			}

			.oneOver {
				background-color: #3381FF;
				color: white;
				padding: 10px 0px;
				border-radius: 5px;
				margin: 10px 0px;
				width: 600rpx;
				text-align: center;
				font-weight: bold;
			}

			.login {
				border: 1px solid $grey;
				box-sizing: border-box;
				width: 100%;
				text-align: center;
				text-align: center;
				padding: 10px 0px;
				border-radius: 5px;
				margin: 10px 0px;
			}

			.phoneLogin {
				box-sizing: border-box;
				border: 1px solid $grey;
				width: 100%;
				text-align: center;
				padding: 10px 0px;
				border-radius: 5px;
				margin: 10px 0px;
			}

			.tips {
				color: $grey;
				font-size: 14px;

				.register {
					color: $blue;
				}
			}
		}

		.back {
			z-index: -1;
			position: absolute;
			top: 0px;
			height: 1000rpx;
			width:750rpx;
			background: linear-gradient(to bottom, #4E8CFF, #80c4ff, rgba(0, 0, 0, 0));
		}
	}
</style>